/**
 * @description 定义通用的状态管理器对象
 */
import { deCodeApi, enCodeApi } from "@/util"
import _ from 'lodash'

const initState = {
    token: '',
    userInfo: {},
    menuList: [],
    rangeList:{},
    rangeInfo:{},
    giftList:{},
    shopList:[],
    userList:[],
    sampleList: {}
}

const common = {
    // 开启命名空间 -- 模块化 -- 也就说在store数据空间中新开一个小的空间
    namespaced: true,
    state: _.cloneDeep(initState),
    getters: {
        // 这里定义的getter方法，Store还需要进行处理
        token(_state) {
            return !!_state.token ? deCodeApi(_state.token) : ''
        },
        userInfo: _state => _state.userInfo,
        menuList: _state => _state.menuList,
        rangeList: _state => _state.rangeList,
        rangeInfo:_state => _state.rangeInfo,
        giftList: _state => _state.giftList,
        shopList:_state => _state.shopList,
        userList:_state => _state.userList,
        sampleList:_state => _state.sampleList,
    },
    mutations: {
        // 这里定义的方法，Store在执行实例化的时候也会进行处理
        // setToken mutation方法，一定有一个入参_state对象
        setToken(_state, _token) {
            _state.token = enCodeApi(_token)
        },
        setUserInfo(_state, _userInfo) {
            _state.userInfo = _userInfo
        },
        setMenuList(_state, _menuList) {
            _state.menuList = _menuList
        },
        setRangeList(_state, _rangeList) {
            _state.rangeList = _rangeList
        },
        setRangeInfo(_state, _rangeInfo) {
            _state.rangeInfo = _rangeInfo
        },
        setGiftList(_state, _giftList) {
            _state.giftList = _giftList
        },
        setShopList(_state,_shopList){
            _state.shopList = _shopList
        },
        setUserList(_state,_userList){
            _state.userList = _userList
        },
        setSampleList(_state,_sampleList){
            _state.sampleList = _sampleList
        },
        // removeUserList(_state){
        //     _state.userList = []
        // },
        clear(_state) {
            _state = _.cloneDeep(initState)
        }
    }
}

export default common